package cn.itcast.zt.java8.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * java8的日志时间格式与Date的转换
 * create by zhangtian105 on 2022/3/3
 */
public class DateUtils {
    private static final String formatter = "yyyy-MM-dd HH:mm:ss";
    private static final ZoneOffset zoneOffset = ZoneOffset.of("+8");

    public static Date asDate(LocalDate localDate) {
        //return Date.from(localDate.atStartOfDay().toInstant(zoneOffset));
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDate localDate, ZoneOffset zoneOffset) {
        return Date.from(localDate.atStartOfDay().toInstant(zoneOffset));
    }

    public static Date asDate(LocalDateTime localDateTime) {
        // return Date.from(localDateTime.toInstant(zoneOffset));
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDateTime localDateTime, ZoneOffset zoneOffset) {
        return Date.from(localDateTime.toInstant(zoneOffset));
    }

    public static Date asDate(ZonedDateTime zonedDateTime) {
        return Date.from(zonedDateTime.toInstant());
    }

    public static LocalDate asLocalDate(Date date) {
        // return date.toInstant().atOffset(zoneOffset).toLocalDate();
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDate asLocalDate(Date date, ZoneOffset zoneOffset) {
        return date.toInstant().atOffset(zoneOffset).toLocalDate();
    }

    public static String asLocalDateStr(Date date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate().format(formatter);
    }

    public static LocalDateTime asLocalDateTime(Date date) {
        // return date.toInstant().atOffset(zoneOffset).toLocalDateTime();
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime asLocalDateTime(Date date, ZoneOffset zoneOffset) {
        return date.toInstant().atOffset(zoneOffset).toLocalDateTime();
    }

    public static String asLocalDateTimeStr(Date date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime().format(formatter) ;
    }

    public static ZonedDateTime asZonedDateTime(Date date) {
        // return ZonedDateTime.ofInstant(date.toInstant(), zoneOffset);
        return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static ZonedDateTime asZonedDateTime(Date date, ZoneOffset zoneOffset) {
        return ZonedDateTime.ofInstant(date.toInstant(), zoneOffset);
    }

    public static String format(LocalDateTime dateTime) {
        return DateTimeFormatter.ofPattern(formatter).format(dateTime);
    }

    public static String format(LocalDateTime dateTime, String formatter) {
        return DateTimeFormatter.ofPattern(formatter).format(dateTime);
    }

    public static LocalDateTime parse(String dateTimeStr) {
        return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(formatter));
    }

    public static LocalDateTime parse(String dateTimeStr, String formatter) {
        return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(formatter));
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.asDate(LocalDateTime.now()));
    }
}
